#!/usr/bin/env python

# Copyright © 2009-2010 marmuta <marmvta@gmail.com>
#
# This file is part of Onboard.
#
# Onboard is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Onboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
from traceback import print_exc

import dbus

from contextlib import contextmanager
@contextmanager
def timeit(s):
    import gc, time
    gc.collect()
    gc.collect()
    gc.collect()
    t = time.time()
    yield None
    print "time: %fms%s" % ((time.time() - t)*1000, " for "+s if s else "")


def main():
    bus = dbus.SessionBus()
    choices = []

    try:
        service = bus.get_object("org.freedesktop.WordPrediction",
                                       "/WordPredictor")

        #iface = dbus.Interface(service, "com.example.SampleInterface")
        #choices = iface.predict(["We", "saw", ""], 20)
        with timeit("client calling Predict"):
            choices = service.predict(["lm:system:en"], "We saw", 20)

    except dbus.DBusException:
        print_exc()
        #sys.exit(1)

    print choices

    if sys.argv[1:] == ['--exit-service']:
        service.Exit()

if __name__ == '__main__':
    main()

